package com.qymerge.manage.core.utils;

import com.qymerge.manage.core.bean.ActionResultRO;
import com.qymerge.manage.core.constant.ParamKey;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Desc 输出字符串
 * User qiuchen
 * Date 14-3-30.
 * Time 上午11:13
 */
public abstract class PrintCodeUtils {


    private PrintCodeUtils() {

    }

    public static void printError(HttpServletResponse response, int code, String msg, String data, String callback) {
        print(response, code, msg, data, callback);
    }

    public static void printError(HttpServletResponse response, int code, String msg, String data) {
        print(response, code, msg, data, null);
    }

    public static void printError(HttpServletResponse response, int code, String msg) {
        print(response, code, msg, "{}", null);
    }

    public static void printError(HttpServletResponse response, String msg) {
        print(response, 0, msg, "{}", null);
    }

    public static void printError(HttpServletResponse response, String msg, String callback) {
        print(response, 0, msg, "{}", callback);
    }

    public static void printError(PrintWriter out, String msg, String callback) {
        print(out, 0, msg, "{}", callback);
    }

    public static void printSuccess(PrintWriter out, String msg, String data, String callback) {
        print(out, 1, msg, data, callback);
    }

    public static void printSuccess(HttpServletResponse response, String data) {
        print(response, 1, "ok", data, null);
    }

    public static void printSuccess(HttpServletResponse response, String data, String callback) {
        print(response, 1, "ok", data, callback);
    }

    public static void printSuccess(HttpServletResponse response, String msg, String data, String callback) {
        print(response, 1, msg, data, callback);
    }

    public static void printSuccess(HttpServletResponse response, Object data, String callback) {
        print(response, 1, "ok", GsonUtil.t2Json2(data,false,false), callback);
    }

    public static String getEscapeString(String str) {
        return ParamKey.Out.quot + str + ParamKey.Out.quot;
    }

    public static void print(HttpServletResponse response, ActionResultRO resultDto, String callback) {
        int code = resultDto.isSuccess() ? 1 : 0;
        String msg = resultDto.getMessage();
        String data = null;
        if (code == 0) {
            data = "{}";   ////
        } else {
            data = resultDto.getResultMap() == null ? "{}" : GsonUtil.t2Json2(resultDto.getResultMap(),false,false);
        }
        print(response, code, msg, data, callback);
    }


    public static void print(HttpServletResponse response, int code, String msg) {
        print(response, code, msg, "{}", null);
    }

    public static void print(HttpServletResponse response, int code, String msg, String data, String callback) {
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
            StringBuilder sb = new StringBuilder("{");
            sb.append(getEscapeString(ParamKey.Out.code))
                    .append(":").append(code).append(",")
                    .append(getEscapeString(ParamKey.Out.msg)).append(":").append(getEscapeString(msg)).append(",")
                    .append(getEscapeString(ParamKey.Out.data)).append(":").append(data)
                    .append("}");
            String result = sb.toString();
            if (callback != null && callback.length() > 0) {
                result = callback + "(" + result + ")";
            }
            out.print(result);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != out) {
                out.close();
            }
        }
    }

    public static void print(PrintWriter out, int code, String msg, String data, String callback) {
        StringBuilder sb = new StringBuilder("{");
        sb.append(getEscapeString(ParamKey.Out.code))
                .append(":").append(code).append(",")
                .append(getEscapeString(ParamKey.Out.msg)).append(":").append(getEscapeString(msg)).append(",")
                .append(getEscapeString(ParamKey.Out.data)).append(":").append(data)
                .append("}");

        String result = sb.toString();

        if (callback != null) {
            result = callback + "(" + result + ")";
        }
        out.println(result);
        out.flush();
        out.close();
    }

    /**
     * 向客户端输出消息
     *
     * @param response 响应流
     * @param msg      消息
     */
    public static void printMsg(HttpServletResponse response, String msg, String callback) {
        response.setContentType("application/json; charset=UTF-8");
        PrintWriter writer = null;
        response.setCharacterEncoding("utf-8");
        try {
            writer = response.getWriter();
            if (callback != null && callback.trim().length() > 0) {
                msg = callback + "(" + msg + ")";
            }
            writer.print(msg);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }

    public static void printStatus(HttpServletResponse response, int status) {
        PrintWriter writer = null;
        response.setCharacterEncoding("utf-8");
        try {
            writer = response.getWriter();
            response.setStatus(status);
            writer.print("access");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                writer.flush();
                writer.close();
            }
        }
    }

}